package cn.puge.mertisfront.controller;


import cn.puge.commonutils.R;
import cn.puge.mertisfront.entity.PugeUser;
import cn.puge.mertisfront.entity.PugeUserLogin;
import cn.puge.mertisfront.service.PugeUserLoginService;
import cn.puge.mertisfront.service.PugeUserService;
import cn.puge.servicebase.exceptionhandler.PuGeiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LIJW
 * @since 2020-08-31
 */
@RestController
@RequestMapping("/mertisfront/puge-user-login")
@CrossOrigin
public class PugeUserLoginController {

     @Autowired
     private PugeUserLoginService pugeUserLoginService;

     @Autowired
     private PugeUserService pugeUserService;

    //Login
    @PostMapping("login")
    public R login(@RequestBody PugeUserLogin pugeUserLogin){

        String id = pugeUserLoginService.getLoginToken(pugeUserLogin);

        return R.ok().data("token",id);
    }


    //info
    @GetMapping("info")
    public R info(@RequestParam("token") String token){
        if (token==null || token.equals("undefined")){
            throw new PuGeiException(50014,"过期了");
        }

        PugeUser pugeUser = pugeUserService.getById(token);


        return R.ok().data("roles","[admin]").data("pugeUser",pugeUser).data("avatar","https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
    }



    //logout
    @GetMapping("logout")
    public R logout(){

        return R.ok();
    }
}

